export class Vec2 {
    x: number;
    y: number;

    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }

    clone(): Vec2 {
        return new Vec2(this.x, this.y);
    }

    subtract(other: Vec2): Vec2 {
        this.x -= other.x;
        this.y -= other.y;
        return this;
    }

    add(other: Vec2): Vec2 {
        this.x += other.x;
        this.y += other.y;
        return this;
    }

    multiply(scalar: number): Vec2 {
        this.x *= scalar;
        this.y *= scalar;
        return this;
    }

    divide(scalar: number): Vec2 {
        if (scalar === 0) {
            throw new Error("Division by zero is not allowed.");
        }
        this.x /= scalar;
        this.y /= scalar;
        return this;
    }

    normalize(): Vec2 {
        const len = this.length();
        this.x /= len;
        this.y /= len;
        return this;
    }

    length(): number {
        return Math.sqrt(this.x * this.x + this.y * this.y);
    }
}